/* =============================================================
 * common-utils.js v1.1.0
 * =============================================================
 * Original written by Ysy
 * =============================================================
 * Date 04/06/2020
 * ============================================================ */

"use strict";
let CommonUtils = {

    /*变量判断------------------------------------------------------------------------------start*/
    /**
     * 判断变量为字符串
     * @param str 需要判断的字符串
     * @returns {boolean}
     */
    isString: function (str) {
        // typeof  对于丰富的对象实例只能返回"Object"字符串  例如：typeof [] 返回的是object
        if (str && Object.is("string", typeof str)) return true;
        return false;
    },

    /**
     * 判断变量为数字
     * @param num 需要判断的数字
     * @returns {boolean}
     */
    isNumber: function (num) {
        if (num && Object.is("number", typeof num)) return true;
        return false;
    },

    /**
     * 判断变量为对象
     * @param obj 需要判断的对象
     * @returns {boolean}
     */
    isObject: function (obj) {
        if (obj && obj instanceof Object) return true;
        return false;
    },

    /**
     * 判断变量为数组
     * @param arr 需要判断的数组
     * @returns {boolean}
     */
    isArray: function (arr) {
        if (arr && arr instanceof Array) return true;
        return false;
    },
    /*变量判断------------------------------------------------------------------------------end*/

    /*浏览器操作------------------------------------------------------------------------------start*/

    /**
     * 获取url中的参数
     * @param key  想要获取的url参数
     * @returns {string|null}
     */
    getUrlParameter: function (key) {
        let reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
        let r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    },

    /**
     * 获取url
     * @returns {string}
     */
    getUrlAll: function () {
        return window.location.href;
    },

    /**
     * 获取url路径
     * @returns {string}
     */
    getUrlPath: function () {
        return window.location.pathname;
    },

    /**
     * 自定义命名下载文件
     * @param url  目标文件地址
     * @param fileName  自定义的文件名
     */
    downloadWithCustomName: function (url, fileName) {
        this.getBlob(url, (blob) => {// 获取blob 格式文件值
            if (window.navigator.msSaveOrOpenBlob) {
                navigator.msSaveBlob(blob, fileName);
                return false;
            }

            let link = document.createElement("a");
            let body = document.querySelector("body");

            link.href = window.URL.createObjectURL(blob);
            link.download = fileName;

            // fix Firefox
            link.style.display = "none";
            body.appendChild(link);

            link.click();
            body.removeChild(link);

            window.URL.revokeObjectURL(link.href);
        });
    },

    /**
     * @Author Ysy
     * @Description // 根据url链接下载图片
     * @Param
     * @Date 15:21 2022/12/8
     **/
     downloadImage:function(imgSrc, fileName) {
        let image = new Image();
        // 解决跨域 Canvas 污染问题
        image.setAttribute("crossOrigin", "anonymous");

        image.onload = function () {

            let canvas = document.createElement("canvas");
            canvas.width = image.width;
            canvas.height = image.height;

            let context = canvas.getContext("2d");
            context.drawImage(image, 0, 0, image.width, image.height);
            let url = canvas.toDataURL("image/png"); //得到图片的base64编码数据
            let a = document.createElement("a"); // 生成一个a元素
            let event = new MouseEvent("click"); // 创建一个单击事件

            a.download = fileName || "photo"; // 设置图片名称
            a.href = url; // 将生成的URL设置为a.href属性
            a.dispatchEvent(event); // 触发a的单击事件
        };

        image.src = imgSrc;
    },

    /**
     * 获取 blob
     * @param url 目标文件地址
     * @param callback 回调函数
     */
    getBlob: function (url, callback) {
        let xhr = new XMLHttpRequest();
        xhr.open("GET", url, true);
        xhr.responseType = "blob";
        xhr.onload = function () {
            if (Object.is(xhr.status, 200)) {
                callback(xhr.response);
            }
        };
        xhr.send();
    },
    /*浏览器操作--------------------------------------------------------------------------------end*/

    /*常用方法--------------------------------------------------------------------------------start*/
    sleep: function (number) {
        let now = new Date();
        let exitTime = now.getTime() + number * 1000;
        while (true) {
            now = new Date();
            if (now.getTime() > exitTime)
                return false;
        }
    },

    copy: function (value) {
        let oInput = document.createElement("input");
        // 这边为链接地址this.liveLink='www.baidu.com'
        oInput.value = value;
        document.body.appendChild(oInput);
        oInput.select();
        document.execCommand("Copy");
        oInput.remove();
    }
    /*常用方法--------------------------------------------------------------------------------end*/

};

